package Silnik;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Jakub Banaszewski
 */
public class MyDate implements Comparable<Object>
{
   private int day;
   private int month;
   private int year;

   public MyDate(int day, int month, int year)
   {
      this.day = day;
      this.month = month;
      this.year = year;
   }

   public MyDate(Date data)
   {
      Calendar cal = Calendar.getInstance();
      cal.setTime(data);
      day = cal.get(Calendar.DATE);
      month = cal.get(Calendar.MONTH) + 1;
      year = cal.get(Calendar.YEAR);
   }

   public int getDay()
   {
      return day;
   }

   public int getMonth()
   {
      return month;
   }

   public int getYear()
   {
      return year;
   }
   
   @Override
   public boolean equals(Object o)
   {
      if (o instanceof MyDate)
         return (compareTo(o) == 0);
      else
         return false;
   }

   @Override
   public int hashCode()
   {
      int hash = 3;
      hash = 59 * hash + this.day;
      hash = 59 * hash + this.month;
      hash = 59 * hash + this.year;
      return hash;
   }
   @Override
   public int compareTo(Object o)
   {
      if (o instanceof MyDate)
      {
         MyDate md = (MyDate) o;
         if (md.getYear() > year)
            return -1;
         else if (md.getYear() < year)
            return 1;
         if (md.getMonth() > month)
            return -1;
         else if (md.getMonth() < month)
            return 1;
         if (md.getDay() > day)
            return -1;
         else if (md.getDay() < day)
            return 1;
         return 0;
      }
      else
      throw new ClassCastException("Wrong class compare");
   }

   public boolean after(MyDate koniec)
   {
      return (this.compareTo(koniec) <= 0) ? false : true;
   }

   public int dayDistance(MyDate startDate)
   {
      Calendar cal = Calendar.getInstance();
      cal.set(year, month-1, day);
      Date end = cal.getTime();
      cal.set(startDate.getYear(), startDate.getMonth()-1, startDate.getDay());
      Date start = cal.getTime();
      return (int) ((end.getTime() - start.getTime()) / (1000 * 60 * 60 * 24));
   }

   public MyDate addDays(int i)
   {
      Calendar cal = Calendar.getInstance();
      cal.set(year, month-1, day);
      cal.add(Calendar.DATE, i);      
      return new MyDate(cal.getTime());
   }

   @Override
   public String toString()
   {
      return day + "/" + month + "/" + year;
   }
   
}
